package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.ModulationInBillDetailMapper;
import com.canaan.business.domain.bill.ModulationInBillDetail;
import com.canaan.business.service.bill.IModulationInBillDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 入库调整单明细Service业务层处理
 *
 * @author canaan
 * @date 2025-03-24
 */
@Service
public class ModulationInBillDetailServiceImpl implements IModulationInBillDetailService {
    @Resource
    private ModulationInBillDetailMapper modulationInBillDetailMapper;

    /**
     * 查询入库调整单明细
     *
     * @param id 入库调整单明细主键
     * @return 入库调整单明细
     */
    @Override
    public ModulationInBillDetail selectModulationInBillDetailById(Long id) {
        return modulationInBillDetailMapper.selectModulationInBillDetailById(id);
    }

    /**
     * 查询入库调整单明细列表
     *
     * @param modulationInBillDetail 入库调整单明细
     * @return 入库调整单明细
     */
    @Override
    public List<ModulationInBillDetail> selectModulationInBillDetailList(ModulationInBillDetail modulationInBillDetail) {
        return modulationInBillDetailMapper.selectModulationInBillDetailList(modulationInBillDetail);
    }

    /**
     * 新增入库调整单明细
     *
     * @param modulationInBillDetail 入库调整单明细
     * @return 结果
     */
    @Override
    public int insertModulationInBillDetail(ModulationInBillDetail modulationInBillDetail) {
        if(StringUtils.isBlank(modulationInBillDetail.getNo())){
            modulationInBillDetail.setNo(TableNoUtil.getNo(ModulationInBillDetail.class));
        }
        if(StringUtils.isEmpty((modulationInBillDetail.getCreateBy()))){
            modulationInBillDetail.setCreateBy(SecurityUtils.getUsername());
        }
        modulationInBillDetail.setCreateTime(DateUtils.getNowDate());
        return modulationInBillDetailMapper.insertModulationInBillDetail(modulationInBillDetail);
    }

    /**
     * 批量新增入库调整单明细
     *
     * @param modulationInBillDetailList 入库调整单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertModulationInBillDetail(List<ModulationInBillDetail> modulationInBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationInBillDetail modulationInBillDetail : modulationInBillDetailList){
            if(StringUtils.isBlank(modulationInBillDetail.getNo())){
                modulationInBillDetail.setNo(TableNoUtil.getNo(ModulationInBillDetail.class));
            }
            modulationInBillDetail.setCreateBy(username);
            modulationInBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<modulationInBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationInBillDetailList.size()) {
                endIndex = modulationInBillDetailList.size();
            }
            insertNum = insertNum + modulationInBillDetailMapper.batchInsertModulationInBillDetail(modulationInBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改入库调整单明细
     *
     * @param modulationInBillDetail 入库调整单明细
     * @return 结果
     */
    @Override
    public int updateModulationInBillDetail(ModulationInBillDetail modulationInBillDetail) {
        modulationInBillDetail.setUpdateBy(SecurityUtils.getUsername());
        modulationInBillDetail.setUpdateTime(DateUtils.getNowDate());
        return modulationInBillDetailMapper.updateModulationInBillDetail(modulationInBillDetail);
    }

    /**
     * 批量修改入库调整单明细
     *
     * @param modulationInBillDetailList 入库调整单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateModulationInBillDetail(List<ModulationInBillDetail> modulationInBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationInBillDetail modulationInBillDetail : modulationInBillDetailList){
            modulationInBillDetail.setUpdateBy(username);
            modulationInBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<modulationInBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationInBillDetailList.size()) {
                endIndex = modulationInBillDetailList.size();
            }
            updateNum = updateNum + modulationInBillDetailMapper.batchUpdateModulationInBillDetail(modulationInBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除入库调整单明细
     *
     * @param ids 需要删除的入库调整单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteModulationInBillDetailByIds(Long[] ids) {
        return modulationInBillDetailMapper.deleteModulationInBillDetailByIds(ids);
    }

    /**
     * 删除入库调整单明细信息
     *
     * @param id 入库调整单明细主键
     * @return 结果
     */
    @Override
    public int deleteModulationInBillDetailById(Long id) {
        return modulationInBillDetailMapper.deleteModulationInBillDetailById(id);
    }

    /**
     * 查询入库调整单明细
     *
     * @param no 编号
     * @return 入库调整单明细
     */
    @Override
    public ModulationInBillDetail selectModulationInBillDetailByNo(String no) {
        return modulationInBillDetailMapper.selectModulationInBillDetailByNo(no);
    }

    /**
     * 批量删除入库调整单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteModulationInBillDetailByNos(String[] nos) {
        return modulationInBillDetailMapper.deleteModulationInBillDetailByNos(nos);
    }

    /**
     * 删除入库调整单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteModulationInBillDetailByNo(String no) {
        return modulationInBillDetailMapper.deleteModulationInBillDetailByNo(no);
    }
}
